

YUI.add("videoPlugin", function(Y) {
    var Lang = Y.Lang,
        Node = Y.Node;

	// A plugin class designed to animate Widget's show and hide methods.
	function VideoPlugin(config) {
		VideoPlugin.superclass.constructor.apply(this, arguments);
	}
	
	// Define Static properties NAME (to identify the class) and NS (to identify the namespace)
	VideoPlugin.NAME = 'videoPlugin';
	VideoPlugin.NS = 'videoPlugin';
	
	// Attribute definitions for the plugin
	VideoPlugin.ATTRS = {
	};
	
	// Extend Plugin.Base
	Y.extend(VideoPlugin, Y.Plugin.Base, {
		
	  // Automatically called by Base, during construction
		initializer: function(config) { 
			Y.on("domready", Y.bind(this.initWidget , this));
			this.videoId = null;
		},
		initWidget : function() {
			this.videoPlayer = new Y.VideoPlayer({ 
				//contentBox: this.get("host").get("contentBox").get('parentNode'),
				visible:false,
				shim:false,
				zIndex:10
			}); 
			this.videoPlayer.render(); 
			//this.videoPlayer.hide();
			this.doBefore('filterMessage', Y.bind( this.filterMessage, this));
			this.doAfter('displayMessage'  , Y.bind( this.bindVideoIcons, this)); 
		},
	 	destructor : function() {
        },
        
        bindVideoIcons : function() {
        	if(!this.videoId)return;
			var playVideoIcons = Node.get('#'+ this.videoId);
			//playVideoIcons.setStyle('background' , '#00ff00');
				
			//Y.on("click", this._onVideoIconClick  , playVideoIcons); 
        	Y.on("click", Y.bind(this._onVideoIconClick , this), playVideoIcons);
        	//	alert("=-=-=-=")
        	
        },
        _onVideoIconClick:function(e){ 
        	this.videoPlayer.show(); 
        	var node = e.target;
        	var id = node.get("id");
        	this.videoPlayer.play(id);
        },
		filterMessage : function(){  
			var msg = this.get("host").get("message");
			var youtubeVideoREGEX= /youtube\.com\/watch\?v=([^&]+)/ ;
			if(! msg.match(youtubeVideoREGEX) )return;
			
			var videoId =  youtubeVideoREGEX.exec(msg)[1]; 
			if(videoId!=null)
			{ 
				
				msg += "<div class='playVideoIcon' id='"+ videoId +"' ></div><br/>"
				this.get("host").set("message" , msg );
				this.videoId = videoId;
			}
			else
			{
				this.videoId = null;
				//this.videoPlayer.hide();
				Y.log("hiding the player"); 
			}
			
			
		}
	});
	Y.VideoPlugin = VideoPlugin;
} , 0.1 );
            